module.exports = function ($scope, $state, $resource, $modal, toaster) {
  $scope.flag = '';
  $scope.getinfo = function(){
    $resource('/api/as/mc/usershop/getInfo', {}, {}).save({}, function (res) {
        if (res.errcode === 0) {
            console.log('已绑定微信');
            console.log(res);
        } else if (res.errcode === 10003) {
            $scope.flag = 'add';
            $resource('/api/ac/mc/merchantUserShopService/getUrl', {}, {}).save({}, function(res){
              if (res.errcode === 0){
                var modalInstance = $modal.open({
                  template: require('../views/skwxOrcode.html'),
                  controller: 'skwxOrcode',
                  size: 'sm',
                  resolve: {
                    qrcode: function () {
                      return res.data.url;
                    }
                  }
                });
                modalInstance.result.then(function () {
                  $scope.loadlist();
                  $scope.flag = '';
                });
              } else {
                toaster.error({title: '', body: '生成二维码失败'});
              }
            });
        }
    });
  }
  $scope.getinfo();
  $scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};
	/* 分页
   * ========================================= */
  $scope.maxSize = 5;            //最多显示多少个按钮
  $scope.bigCurrentPage = 1;      //当前页码
  $scope.itemsPerPage = 10; 
  $scope.searchform = {};
	//绑定列表
	$scope.loadlist = function () {
    var para = {
        pageNo:$scope.bigCurrentPage, 
        pageSize:$scope.itemsPerPage,
    };
    para = angular.extend($scope.searchform, para);
		$resource('/api/as/mc/usershop/findList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope.userList = res.data.results;
				$scope.bigTotalItems = res.data.totalRecord;
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

	$scope.loadlist();

	//添加人员
	$scope.add = function () {
    $resource('/api/ac/mc/merchantUserShopService/getUrl', {}, {}).save({}, function(res){
      if (res.errcode === 0){
        var modalInstance = $modal.open({
          template: require('../views/skwxOrcode.html'),
          controller: 'skwxOrcode',
          size: 'sm',
          resolve: {
            qrcode: function () {
              return res.data.url;
            }
          }
        });
        modalInstance.result.then(function () {
          $scope.loadlist();
          $scope.flag = '';
        });
      } else {
        toaster.error({title: '', body: '生成二维码失败'});
      }
    });
		
	}

	//解绑
	$scope.delete = function (bean) {
    var modalInstance = $modal.open({
			template: require('../views/skwxUntie.html'),
			controller: 'skwxUntie',
			size: 'lg',
			resolve: {
				// bean: function () {
				// 	return bean;
				// }
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
      $scope.loadlist();
      $scope.flag = 'add';
		});
		
  }
  $scope.withdraw = function(){
      var modalInstance = $modal.open({
        template: require('../views/skwxwithdraw.html'),
        controller: 'skwxwithdraw',
        size: 'lg',
        resolve: {
          // bean: function () {
          // 	return bean;
          // }
        }
      });
      //关闭模态框刷新页面
      modalInstance.result.then(function () {
        $scope.loadlist();
        $scope.flag = '';
      });
  }
  // 提现记录
  $scope.withdarwList = function(){
      $state.go('app.skwxWithdrawList');
  }

	//设置首页显示不显示
	// $scope.change = function(info) {
		
	// 	var para = {
	// 		'class_state': info.selected ? '1' : '0',
	// 		'id': info.id
	// 	}
	// 	$resource('/api/as/mc/shopclass/update', {}, {}).save(para, function(res){
	// 		if(res.errcode === 0){
	// 			toaster.success({title: '', body: '设置成功。'});
	// 		}else{
	// 			toaster.error({title: '', body: '设置失败请重试。'});
	// 		}
	// 	});
	// }
};